#include "symbols.h"

char *Symbols::chars = NULL;

Symbols::Symbols( void )
	: gray_depth(13), top_id( gray_depth - 1 )
{
	chars = new char[gray_depth];
	chars[0] = '@';
	chars[1] = 'M';
	chars[2] = 'B';
	chars[3] = 'K';
	chars[4] = 'X';
	chars[5] = 't';
	chars[6] = 'Y';
	chars[7] = 'L';
	chars[8] = 'i';
	chars[9] = '+';
	chars[10] = '-';
	chars[11] = '.';
	chars[12] = ' ';
}

char Symbols::operator[]( const Color& color )
{
	int id = get_id( color );
	return chars[id];
}

int Symbols::get_id( const Color& color )
{
	ARGB new_color = alpha_as_white( color );	
	// calc id
	double brightness = (double)new_color / 0x00ffffff; // get brightness of pixel
	int id = (int)(brightness * top_id);
	return id;
}

ARGB Symbols::alpha_as_white(const Color &color)
{
	BYTE alpha = color.GetA();
	BYTE red = color.GetR();
	BYTE green = color.GetG();
	BYTE blue = color.GetB();

	alpha = ~alpha; // now 255 mean full transparency, 0 mean lack of transparency
	// imagine that our semi-transparent image rests on the white surface
	// so alpha channel represents white color
	// we should add this white color to all channels
	int tmp = red + alpha;
	red = min( tmp, 255 );

	tmp = green + alpha;
	green = min( tmp, 255 );

	tmp = blue + alpha;
	blue = min( tmp, 255 );

	//making new color
	ARGB new_color = Color::MakeARGB( 0, red, green, blue );

	return new_color;
}